package com.yld.demo2.controller;


import com.yld.demo2.entity.table.OrderList;
import com.yld.demo2.entity.vo.OrderListVo;
import com.yld.demo2.service.OrderListService;
import com.yld.demo2.util.DataUtil;
import com.yld.demo2.util.DateFormatConversion;
import com.yld.demo2.util.tooltips.ResBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.sql.Date;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class OrderListController {

    //关联数据
    List<String> userIdList = new ArrayList<>();

    @Autowired
    OrderListService orderListService;

    @GetMapping("/selectByUserId/{userid}")
    @ApiOperation("根据用户编号user_id查询")
    public ResBean selectByUserId(@PathVariable("userid") Integer userid){
 //       OrderListVo OLvo;
//        try {
//            Date sDate = DateFormatConversion.strToDate(startDate);
//            Date eDate = DateFormatConversion.strToDate(endDate);
//        } catch (ParseException e) {
//            return ResBean.formatDoesNotMatch();
//        }

        OrderListVo orderListVo = orderListService.selectByUserId(userid);
        System.out.println(orderListVo.toString());
        return ResBean.success(orderListVo);

//        //根据 关联数据 查询 redis
//        if ((userIdList = redisTemplate2.opsForList().range(userid,0,userIdList.size())) != null){
//            return ResBean.success(redisTemplate1.opsForValue().get(userIdList));
//        }
//        //直接查询 数据库
//        if ((OLvo = orderListService.selectByUserId(userid)) != null){
//            redisTemplate2.opsForList().leftPushAll(userid, OLvo.getOrderno());
//        }

    }

    @GetMapping("/selectAll")
    @ApiOperation("查询全部")
    public ResBean selectAll(){
        //       OrderListVo OLvo;
//        try {
//            Date sDate = DateFormatConversion.strToDate(startDate);
//            Date eDate = DateFormatConversion.strToDate(endDate);
//        } catch (ParseException e) {
//            return ResBean.formatDoesNotMatch();
//        }
        List<OrderListVo> orderListVos = orderListService.selectAll();

        System.out.println(orderListVos);

        return ResBean.success(orderListVos);

//        //根据 关联数据 查询 redis
//        if ((userIdList = redisTemplate2.opsForList().range(userid,0,userIdList.size())) != null){
//            return ResBean.success(redisTemplate1.opsForValue().get(userIdList));
//        }
//        //直接查询 数据库
//        if ((OLvo = orderListService.selectByUserId(userid)) != null){
//            redisTemplate2.opsForList().leftPushAll(userid, OLvo.getOrderno());
//        }

    }



    @PutMapping("/insertAll")
    @ApiOperation("添加方法 添加所有")
    public ResBean insertAll(@RequestBody OrderList orderList){

        if (orderList.getOrdernum() == 0 | orderList.getAmout() == 0){
            return ResBean.numberOrMoneyNotZero();
        }
        //获取当前 年月日
        OrderListVo orderListVo = DataUtil.nowDate(orderList);
        //数据库添加
        Boolean aBoolean = orderListService.insertAll(orderListVo);
//        //redis添加
//        redisTemplate1.opsForValue().set(orderListVo.getOrderno(),orderListVo);

        return ResBean.success(aBoolean);
    }


    @ApiOperation("根据订单号order_no 删除")
    @DeleteMapping("/deleteByOrderNo/{orderno}")
    public ResBean deleteByOrderNo(@PathVariable Integer orderno){
//        //redis 或 数据库 数据不存在情况
//        if (redisTemplate1.opsForValue().get(orderno) == null){
//
//            if (orderListService.selectByUserId(orderno) == null){
//                return ResBean.noData();
//            }
//            return ResBean.success("redis与数据库 信息不同步 请检查联系管理员 检查系统");
//        }
//        //清除redis数据
//        redisTemplate1.delete(orderno);
        //根据 参数 删除数据库
        if (orderListService.selectByUserId(orderno) == null){
                return ResBean.FAIL_DML();
        }else {
            return ResBean.success(orderListService.deleteByOrderNo(orderno));
        }

    }


    @ApiOperation("根据订单号order_no 修改")
    @PutMapping("/updateByOrderNo")
    public ResBean updateByOrderNo(@RequestBody OrderListVo OrderListVo){
//        //redis 或 数据库 数据不存在情况
//        if (redisTemplate1.opsForValue().get(orderListVo.getOrderno()) == null){
//            if (orderListService.selectByUserId(orderListVo.getOrderno()) == null){
//                return ResBean.noData();
//            }
//            return ResBean.success("redis与数据库 信息不同步 请检查联系管理员 检查系统");
//        }
//        //清楚redis中的数据
//        redisTemplate1.delete(orderListVo.getOrderno());
        //判断要修改的 数据 在数据库中 是否存在
        if (orderListService.selectByUserId(OrderListVo.getOrderno()) == null){
            return ResBean.noData();
        }
//        // 金额 和 交易数量 不能为0
//        if (orderListVo.getOrdernum() == 0 | orderListVo.getAmout() == 0){
//            return ResBean.numberOrMoneyNotZero();
//        }
        //数据库的修改操作
        Boolean aBoolean = orderListService.updateByOrderNo(OrderListVo);
//        //将修改后的数据 添加进redis
//        redisTemplate1.opsForValue().set(orderListVo.getOrderno(),orderListVo);

        return ResBean.success(aBoolean);
    }



}
